package com.bjpowernode.controller;

import com.bjpowernode.domain.Student;
import com.bjpowernode.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/addStudent.do")
    public ModelAndView addStudent(Student student) {
        ModelAndView mv = new ModelAndView();

        // 调用service
        Integer rows = studentService.addStudent(student);
        String msg = "注册失败";
        if (rows != null) {
            msg = "注册成功";
        }
        mv.addObject("msg", student.getName() + "," + msg);
        mv.setViewName("result");
        return mv;
    }

    @RequestMapping("/listStudent.do")
    @ResponseBody
    public List<Student> queryStudent() {
        List<Student> students = studentService.queryStudents();
        return students;
    }
}
